package cc.rengu.redp.bizimpl.upmp.controller;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.igas.bean.InstInfo;
import cc.rengu.redp.bizimpl.igas.vo.InstInfoListQueryRespVO;
import cc.rengu.redp.bizimpl.upmp.domain.BatchExcpCfrm;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.service.BatchExcpCfrmService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.RedpResponse;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
  *	对BatchExcpCfrm实体进行管理的控制器
  
  * @author zhuyaping
  * @date 2020年09月21日09时34分
  *
  *	@see 
  */
@Slf4j
@Api("批次异常确认管理")
@RestController
@RequestMapping(value = "/admin/batchExcpCfrm")
public class BatchExcpCfrmController extends BaseController {

    @Autowired
    private BatchExcpCfrmService batchExcpCfrmService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @ApiOperation("批次异常确认管理:法人行查询")
    @GetMapping("/instList")
    //@RequiresPermissions("areaInfo:view")
    public InstInfoListQueryRespVO instInfoList() {
        UpmpSysParam defaultInst = upmpSysParamService.getById(UpmpConstant.DEAFULT_INST_PARAM_ID);
        List<UpmpSysParam> sysParamList = upmpSysParamService.findChildrenByPrimaryKey(UpmpConstant.SYS_INST_PARAM, defaultInst.getInstId(), null);
        List<InstInfo> instList = Lists.newArrayList();
        sysParamList.forEach(inst -> {
            InstInfo instInfo = new InstInfo(inst.getPrimaryKey(), inst.getPrimaryValue());
            instList.add(instInfo);
        });
        InstInfoListQueryRespVO resp = new InstInfoListQueryRespVO();
        resp.setInstInfoList(instList);
        return resp;

    }

    @ApiOperation("批次异常确认管理:异常任务查询")
    @RequiresPermissions("batchExcpCfrm:view")
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> findByPage(HttpServletRequest request) {
        return getDataTable(this.batchExcpCfrmService.findList(request));
    }

    @ApiOperation("批次异常确认管理:异常任务处理")
    @RequiresPermissions("batchExcpCfrm:view")
    @PostMapping("/edit")
    @ResponseBody
    public RedpResponse editSave(HttpServletRequest request) {
        try {
            batchExcpCfrmService.saveData(request);
        }catch (Exception e){
            String message = "数据处理失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }
        return RedpResponse.getInstance().success().message("数据处理完成");

        //TODO 添加实际业务操作
    }

}